//  app/db/business/ActivityOptions.js
const { app } = require('electron');
const path = require('path');
const Database = require('better-sqlite3');
const logger = require('@utils/logger');
const colors = require('colors')

const Activity = require('@db/entity/ActivityEntity');


class ActivityController {
    constructor() {
        this.entity = new Activity();
    }

    /**
     * 条件查询，分页
     * @param {Object} filters 查询条件
     * @param {number} page 当前页码
     * @param {Number} pageSize 每页条数
     * @returns 
     */
    getActivitiesByFilter(filters, page = 1, pageSize = 10) {
        let query = this.entity.query().select('*')
        // console.log('controller filters', filters);
        // console.log('controller filters.state', filters.state);

        if (filters.state) {
            query = query.where('state', '$eq', filters.state)
        }

        if (filters.serial) {
            query = query.where('serial', '$eq', filters.serial)
        }

        if (filters.name) {
            query = query.where('name', '$like', filters.name)
        }

        if (filters.is_deleted == this.entity.STATUS.DELETE_STATUS.DELETED) {
            query = query.recycled();
        } else {
            query = query.withoutDeleted();
        }

        const activities = query.paginate(page, pageSize).execute();
        // console.log('activities::'.blue, activities);

        // 2. 获取总数
        const total = this.entity
            .query()
            .where('state', '$eq', 1)
            .getTotal();
        // console.log('total::'.red, total);
        return { activities, total };
    }

    // 根据 id 获取所有活动
    gettActivitiesByID(id) {
        const sql = `
            SELECT * FROM activities
            WHERE id = @id
            ORDER BY sort_by DESC, id ASC
        `;
        return this.entity.db.prepare(sql).all({ 'id': id });
    }

    // 根据 id 获取活动
    getActivityById(id) {
        return this.entity.getById(id);
    }

    // 插入新活动
    insertActivity(activity) {
        // console.log(`ActivityController.js 插入新活动调试：${JSON.stringify(activity)}`.blue)
        return this.entity.insert(activity);
    }

    // 更新活动
    updateActivity(activity) {
        return this.entity.update(activity);
    }

    // 删除活动
    deleteActivity(id) {
        const activity = this.getActivityById(id);
        if (activity === undefined) return 0;
        // console.log(`删除活动调试：${id}`.blue, activity)
        if (activity.is_deleted === 1) {
            return this.entity.delete(id);
        } else {
            return this.entity.remove(id);
        }
    }

    // 根据条件筛选活动
    filtertActivities(acitivityId, filters) {
        let sql = `
            SELECT * FROM activities
            WHERE id = @acitivityId
        `;

        if (filters.state) {
            sql += ` AND state = ${filters.state}  `;
        }

        if (filters.name) {
            sql += ` AND name LIKE '%${filters.name}%'`;
        }


        sql += ` ORDER BY  id DESC`;

        return this.favorModel.db.prepare(sql).all({ acitivityId });
    }
}

module.exports = ActivityController;